package com.classeven.downhill.state;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;

import android.view.KeyEvent;

import com.classeven.downhill.define.iDefine_Object;
import com.classeven.downhill.define.iGameDefines;
import com.classeven.downhill.element.ABackground;
import com.classeven.downhill.element.AScene;
import com.classeven.downhill.element.ASprite;
import com.classeven.downhill.element.AText;
import com.classeven.downhill.engine.GameEngine;
import com.classeven.downhill.engine.Utils;
import com.classeven.downhill.object.Mc;
import com.classeven.downhill.object.Obstacle;

public class State_GamePlay {

	GameEngine engine;
	static AScene scene;
	ABackground bg;
	Mc mc;
	Obstacle obs;
	ASprite live, bg_score, btn_pause;
	
	AText[] cDown = new AText[3]; 
	AText txt_live, go, distance, minutes, second;
	
	private boolean countDown;
	private float speed =2;
	
	private float scale = 5, scale1 = 5, scale2 = 5, scale3 = 5;
	
	public float pixDisc , Disc;
	
	long updateSec, milSec;
	int sec, min;
	
	public State_GamePlay(GameEngine engine)
	{
		this.engine = engine;
		
		
		bg = new ABackground(engine, iGameDefines.SPRITE_GAME, 0);
		mc = new Mc(engine);
		obs = new Obstacle(engine, 5, engine.CAMERA_HEIGHT );
		bg_score = new ASprite(engine, iGameDefines.SPRITE_GAME, iGameDefines.GAME_BG_SCORE);
		btn_pause = new ASprite(engine, iGameDefines.SPRITE_GAME, iGameDefines.GAME_BTN_PAUSE);
		scene = new AScene(0);
		
		for(int i=0; i < cDown.length; i++)
		{
			cDown[i] = new AText(engine, iGameDefines.TEXT_COUNT_DOWN, 0, engine.gameFont[0] );
		}
		
		go = new AText(engine, iGameDefines.TEXT_COUNT_DOWN, 1, engine.gameFont[0] );
		distance = new AText(engine, iGameDefines.TEXT_GAME, 0, engine.gameFont[1]);
		
		txt_live = new AText(engine, iGameDefines.TEXT_GAME, 1, engine.gameFont[1]);
		live = new ASprite(engine, iGameDefines.SPRITE_GAME, iGameDefines.GAME_LIVE);
		
	
		minutes = new AText(engine, iGameDefines.TEXT_GAME, 2, engine.gameFont[1]);
		second = new AText(engine, iGameDefines.TEXT_GAME, 3, engine.gameFont[1]);
	}
	
	public void init()
	{
		scene.theScene().setOnAreaTouchListener(engine);
		scene.theScene().setOnSceneTouchListener(engine);
		
		engine.SetGameScene(scene);
		engine.SceneAttach(scene, bg);
		obs.attach(scene);
		engine.SceneAttach(scene, mc.getMc());
		
		engine.SceneAttach(scene, bg_score);
		engine.SceneAttach(scene, btn_pause);
		
		for(int i=0; i < cDown.length; i++)
		{
			cDown[i].setText(String.valueOf(i+1));
			engine.SceneAttach(scene, cDown[i]);
			cDown[i].theText().setVisible(false);
		}
		
		engine.SceneAttach(scene, go);
		
		go.theText().setVisible(false);
		
		engine.SceneAttach(scene, txt_live);
		engine.SceneAttach(scene, live);
		
		engine.SceneAttach(scene, second);
		engine.SceneAttach(scene, minutes);
		
		engine.SceneAttach(scene, distance);
		
		scene.theScene().registerTouchArea(btn_pause.theSprite());
		scene.theScene().setOnSceneTouchListener(engine);
		scene.theScene().setOnAreaTouchListener(engine);
		
	}
	
	public void update()
	{
		
		generateCountDown();
		
		if(countDown == false)
		{
			generateTimer();
			
//			Utils.TRACE("Min "+milSec);
//			Utils.TRACE("sec "+sec);
			
			obs.update(scene, speed);
//			engine.getEngine().registerUpdateHandler(new FPSLogger());
		
			mc.onUpdate();
			getDistance(speed);

			distance.setText(String.valueOf(Disc));
		}
	}
	
	public void exit()
	{
		scene.theScene().detachChildren();
	}

	public void back(int keyCode, KeyEvent event)
	{
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			
			engine.SetNextMessageState(0);
			engine.SetNextGameState(iGameDefines.GAME_STATE_PAUSE);
		}
		
	}

	public void event(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea,
			float pTouchAreaLocalX, float pTouchAreaLocalY) 
	{
		if(pTouchArea.equals(btn_pause.theSprite()) && pSceneTouchEvent.isActionDown())
		{
			engine.SetNextMessageState(0);
			engine.SetNextGameState(iGameDefines.GAME_STATE_PAUSE);
		}
	}
	
	private void generateCountDown()
	{
		mc.setMove(false);
		countDown = true;
		
		float minScale = 0.05f;
		
				
		if(countDown)
		{
//			Utils.TRACE("scale, "+cDown[2].theText().getScaleX());
			
			cDown[2].theText().setVisible(true);
			cDown[2].theText().setScale(scale -= minScale);
			
			if(cDown[2].theText().getScaleX() <= 0.3f )
			{
				
				cDown[2].theText().detachSelf();
				cDown[1].theText().setVisible(true);
				cDown[1].theText().setScale(scale1 -= minScale);
			}
			if(cDown[1].theText().getScaleX() <= 0.3f && cDown[1].theText().isVisible())
			{
				
				cDown[1].theText().detachSelf();
				cDown[0].theText().setVisible(true);
				cDown[0].theText().setScale(scale2 -= minScale);
			}
			if(cDown[0].theText().getScaleX() <= 0.3f && cDown[0].theText().isVisible())
			{
				cDown[0].theText().detachSelf();
				go.theText().setVisible(true);
				go.theText().setScale(scale3 -= minScale);
			}
			if(go.theText().getScaleX() <= 0.3f && go.theText().isVisible())
			{
				go.theText().detachSelf();
				countDown = false;
				mc.setMove(true);
			}
		}
		
		
		
	}

	public void sceneTouch(Scene pScene, TouchEvent pSceneTouchEvent)
	{
		if(pSceneTouchEvent.getX() <= GameEngine.CAMERA_WIDTH / 2 && pSceneTouchEvent.isActionDown())
		{
//			speed
			
			speed 	= 3;
		}
		else if(pSceneTouchEvent.getX() <= GameEngine.CAMERA_WIDTH /2 && pSceneTouchEvent.isActionUp())
		{
			speed	=2;
		}
		
		else if(pSceneTouchEvent.getX() >= GameEngine.CAMERA_WIDTH / 2 && pSceneTouchEvent.isActionDown())
		{
//			Utils.TRACE("gggggggggggggggggggggggggggg");
			mc.setState(iDefine_Object.MC_JUMP);
		}
		
//		Utils.TRACE("Touch = "+pSceneTouchEvent.getX());
		
	}
	
	public void getDistance(float speed)
	{
				
		pixDisc += Utils.GetRatio(speed);
		
		if((int) pixDisc % (int) Utils.GetRatioHeight(20) == 0)
		{
			Disc++;
		}
		
		
		
		
	}

	
	
	public void generateTimer()
	{
		engine.getEngine().registerUpdateHandler(new TimerHandler(0.5f, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler)
			{
				// TODO Auto-generated method stub
							
				milSec++;
				if(milSec == 60)
				{
					milSec=0;
					sec++;
					
					if(sec == 60)
					{
						sec =0;
						min++;
					}
				}
//		Utils.TRACE("Minut = "+min);
//		Utils.TRACE("Second = "+sec);
	
		
		second.setText(String.valueOf(sec) +":"+ String.valueOf(milSec));
		minutes.setText(String.valueOf(min) + ":");
			}
		}));
		
	
		
		
	}
}
